<?

class Ajax extends Controller {

	private $config_values;

	function __construct() {
		parent::Controller();
		$this->load->model('Wiki_model');
		$this->load->helper('html');
		$this->load->library('Spyc');
		$path = "cfg/config.yaml";
		$this->config_values = $this->spyc->YAMLLoad($path);

	}

	function history($wiki_word) {

		if ($wiki_group_id = $this->Wiki_model->get_wiki_group_id($wiki_word)) {
	                $history = $this->Wiki_model->get_condensed_wiki_history($wiki_word);
			$this->load->view('ajax/history',array('wiki_versions'=>$history));
		} else {
			return false;
		}

        }

	function versioned($wiki_id, $marked = "bare") {

		$wiki = $this->Wiki_model->get_wiki_by_wiki_id($wiki_id);
		if ($marked == "marked") {
			$wiki = $this->Wiki_model->prepare_wiki($wiki);
		}
		echo $wiki['content'];

	}

        function textbox($wiki_word,$marked = "bare",$hover="nohover") {
                $data['wiki'] = $this->Wiki_model->get_wiki_by_wiki_word($wiki_word,1);
		if ($hover == "hover") {
			$e = explode(' ', $data['wiki']['content']);
			$w = count($e);
			if ($w > $this->config_values['max_words']) {
				$chopped = '';
				for ($i=0;$i<$this->config_values['max_words'];$i++) {
					$chopped .= $e[$i].' ';
				}
				$chopped .= ". . .";
			} else {
				$chopped = $data['wiki']['content'];
			}

			$data['wiki']['content'] = $chopped;
		}
                if ($marked == "marked") {
                        $data['wiki'] = $this->Wiki_model->prepare_wiki($data['wiki']);
                }
                echo $data['wiki']['content'];
        }


        function create($wiki_word = null) {
                #only called through ajax
                if (is_null($wiki_word)) {
                        $data['error'] = "Must supply a wiki name to create one!  Duh!";
                        $this->load->view('error',$data);
                } else {
                        $this->load->library('authenticate');
                        if (!($author = $this->authenticate->is_authenticated())) {
                                $this->load->helper('url');
                                redirect('author/login/'.$wiki_word);
                        }

			if ($this->Wiki_model->check_valid_wiki_word($wiki_word)) {
	                        if (!$wiki_group_id = $this->Wiki_model->get_wiki_group_id($wiki_word)) {
	                                $wiki_group_id = $this->Wiki_model->create_wiki_group($wiki_word,$author);
					$breadcrumbs = $this->Wiki_model->add_bread_crumbs($wiki_word,$author,$this->config_values['max_bread_crumbs']);
       		                 } else {
					$this->Wiki_model->claim_last_edit($wiki_word,$author);
				}
	
				$wiki = $this->Wiki_model->create_wiki($wiki_group_id,$author);
				$wiki = $this->Wiki_model->prepare_wiki($wiki);
#      	                 $this->load->view('json',array('json'=>$wiki['content']));
				$this->load->view('json',array('json'=>json_encode($wiki)));
#                       $this->load->helper('url');
#                       redirect('wiki/view/'.$wiki_word);

                	} else {
				$data['error'] = "This is not a valid WikiWord.";
				$this->load->view('error',$data);
			}
		}

        }

        function tags($wiki_word = null) {

                if (is_null($wiki_word)) {
                        return;
                }

                $tags = $this->Wiki_model->get_tags_by_wiki_word($wiki_word);
//              $this->load->view('json',array('json'=>json_encode($tags)));
		$t = array(); // Strip the tags out of the array of arrays.
		foreach ($tags as $tag) {
			$t[] = $tag['tag'];
		}
		echo implode(", ",$t);

        }


        function create_tags($wiki_word = null) {
                if (is_null($wiki_word)) {
                        return;
                }
                if ($tags = $this->Wiki_model->create_tags($wiki_word)) {
/*			$t = array(); // Strip the tags out of the array of arrays.
			foreach ($tags as $tag) {
				$t[] = $tag['tag'];
			}
			echo implode(", ",$t);*/

                       $this->load->view('json',array('json'=>json_encode($tags)));
                } else {
                        return;
                }
        }

	function set_style($wiki_word = null) {
		if (is_null($wiki_word)) {
			return;
		}
		$wiki = $this->Wiki_model->get_wiki_by_wiki_word($wiki_word);
		//bit of a cludge
		if ($wiki['username'] == 'You') {
			return;
		}

		$this->Wiki_model->update_config('WikiStyle',$wiki_word);
	}

	function get_style($wiki_word = null) {
		if (is_null($wiki_word)) {
			return;
		}
		return $this->Wiki_model->get_style($wiki_word);
	}

	function revert($wiki_id) {
		if ($wiki = $this->Wiki_model->get_wiki_by_wiki_id($wiki_id)) {
			$wiki_word = $this->Wiki_model->get_wiki_word_by_wiki_id($wiki_id);
	                $this->load->library('authenticate');
                        if (!($author = $this->authenticate->is_authenticated())) {
                                $this->load->helper('url');
                                redirect('author/login/'.$wiki_word);
                        }

                        if (!$wiki_group_id = $this->Wiki_model->get_wiki_group_id($wiki_word)) {
				return false;
                        } else {
				$this->Wiki_model->claim_last_edit($wiki_word,$author);
			}

                        $wiki = $this->Wiki_model->create_wiki($wiki_group_id,$author,$wiki['content']);
			$wiki = $this->Wiki_model->prepare_wiki($wiki);
			echo $wiki['content'];
#			$this->load->view('json',array('json'=>json_encode($wiki)));
#			return true;
		} else {
			return false;
		}
	}
}


